#include "def.h"
#include "2440addr.h"
#include "keyScan.h"

static void KeyDely(U32 tt)
{
   U32 i;
   for(;tt>0;tt--)
   {
     for(i=0;i<1000;i++){}
   }
}

static uint8 KeyRead( void )
{
	uint16 data;
	data = rGPGDAT;
	if(      (data&(1<< 0)) == 0 )	
		return KEY1 ;
	else if( (data&(1<< 3)) == 0 )
		return KEY2;
	else if( (data&(1<< 5)) == 0 )
		return KEY3 ;
	else if( (data&(1<< 6)) == 0 )
		return KEY4 ;
	else if( (data&(1<< 7)) == 0 )
		return KEY5 ;
	else if( (data&(1<<11)) == 0 )
		return KEY6 ;
	else
		return NOKEY;
}

void KeyInit(void)
{
	rGPGCON = rGPGCON & (~((0x03<<0 ) | (0x03<<6 ) | (0x03<<10) |\
						   (0x03<<12) | (0x03<<14) | (0x03<<22) ));
	rGPGUP = rGPGUP | ((1<<0) | (1<<3) | (7<<5) | (1<<11));
}
uint8 GetKey(uint8 t)
{
	uint8 key;
	if(KeyRead()== NOKEY)
	{
		KeyDely(1000);
		return 0;
	}
	key = KeyRead();
	KeyDely(1000);
	if(key == KeyRead())
		return key;
	else
		return 0;
}

void WaitKeyPop(void)
{
	int	i;
	for(i=20; i; i--)
	{
		while(KeyRead() != NOKEY);
	}
}